clear all;
close all;
xk1_1=1;xk2=1;xk2_1=1;xk3_1=1;xk4_1=1;
nu1_1=0;nu1_2=0;ut_1=0;ut_2=0;nu2_1=0;nu2_2=0;nu3_1=0;nu3_2=0;nu4_1=0;nu4_2=0;
T=0.001;
for k=1:1:10000
time(k)=k*T;
if time(k)<=5
    dt(k)=0.1*sin(3*k*T);
elseif time(k)>5
    dt(k)=0.2*cos(5*k*T);
end
if time(k)<=2
    Zeta(k)=(1-cos(pi*k*T/2))/2;
elseif time(k)>2
    Zeta(k)=1;
end
if time(k)<=2
    dZeta(k)=(pi*sin(pi*k*T/2))/(2*2);
elseif time(k)>2
    dZeta(k)=0;
end

tSpan=[0 T];

para1_1=[nu1_1,nu1_2];      % D/A
[tt,xx]=ode45('s',tSpan,xk1_1,[],para1_1);
xk1_1=xx(length(xx),:);    % A/D
s1_1(k)=xk1_1(1);

para2_1=[nu2_1,nu2_2];      % D/A
[tt,xx]=ode45('s',tSpan,xk2_1,[],para2_1);
xk2_1=xx(length(xx),:);    % A/D
s2_1(k)=xk2_1(1);

para3_1=[nu3_1,nu3_2];      % D/A
[tt,xx]=ode45('s',tSpan,xk3_1,[],para3_1);
xk3_1=xx(length(xx),:);    % A/D
s3_1(k)=xk3_1(1);

para4_1=[nu4_1,nu4_2];      % D/A
[tt,xx]=ode45('s',tSpan,xk4_1,[],para4_1);
xk4_1=xx(length(xx),:);    % A/D
s4_1(k)=xk4_1(1);

para2=[ut_1,ut_2];      % D/A
[tt,xx]=ode45('z',tSpan,xk2,[],para2);
xk2=xx(length(xx),:);    % A/D
z(k)=xk2(1);

es=s4_1(k)-z(k);
tf=2;delta=0.00001;
kg=dZeta(k)/(1-Zeta(k)+delta);

Kcc=1*log(abs(s1_1(k))/0.01366+1);
Kcv1=5*atan(abs(s2_1(k))/0.05)/(pi/2-atan(abs(s2_1(k))/0.05));
Kcv2=5*atan(abs(s3_1(k))/0.05)/(pi/2-atan(abs(s3_1(k))/0.05));
Kcves=5*atan(abs(es)/0.05)/(pi/2-atan(abs(es)/0.05));
if Kcv2<=5
    Kbf=Kcv2;
elseif Kcv2>5
    Kbf=5;
end


ut1(k)=-Kcc*sign(s1_1(k));
ut2(k)=-Kcv1*sign(s2_1(k));
ut3(k)=-Kbf*sign(s3_1(k));
ut4(k)=-kg*z(k)-Kcves*sign(es);

nu1_1=ut1(k);
nu1_2=dt(k);
nu2_1=ut2(k);
nu2_2=dt(k);
nu3_1=ut3(k);
nu3_2=dt(k);
nu4_1=ut4(k);
nu4_2=dt(k);
ut_1=Zeta(k);
ut_2=dZeta(k);
end
figure(1);
plot(time,s1_1,'k',time,s2_1,'b',time,s3_1,'g',time,s4_1,'r');
legend('CONTROL(9)','CONTROL(13)','CONTROL(18)','CONTROL(25)');
xlabel('time(s)');ylabel('sliding variables');

figure(2);
plot(time,ut1,'k',time,ut2,'b',time,ut3,'g',time,ut4,'r');
legend('CONTROL(9)','CONTROL(13)','CONTROL(18)','CONTROL(25)');
xlabel('time(s)');ylabel('Control input');